﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCrawlerLib;
using HtmlAgilityPack;
using PhotobucketNet;

namespace WCrawler.TestPages
{
    public partial class frmPhotobucket : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtImageUrl.Text = "http://ia.media-imdb.com/images/M/MV5BNDE2MjMyODAyMV5BMl5BanBnXkFtZTcwNjMzMjA4NQ@@._V1._SX640_SY948_.jpg";
                txtAlbum.Text = "UploadImage";
            }
        }

        public string GetTS(HtmlDocument doc)
        {
            string value = "";
            HtmlNode node = doc.DocumentNode.SelectSingleNode(".//input[@type='hidden' and @name='ts']");
            if (node != null)
                value = node.Attributes["value"].Value;
            return value;
        }

        public string GetSIG(HtmlDocument doc)
        {
            string value = "";
            HtmlNode node = doc.DocumentNode.SelectSingleNode(".//input[@type='hidden' and @name='sig']");
            if (node != null)
                value = node.Attributes["value"].Value;
            return value;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //string imageUrl = txtImageUrl.Text;
            //string[] splitPath = imageUrl.Split(new char[]{'/'});
            //string imageName = splitPath[splitPath.Length - 1];

            //string albumName = txtAlbum.Text;
            //string username = txtUserName.Text;
            //string password = txtPassword.Text;

            //string url = "http://photobucket.com/login";
            //string loginUrl = "https://login.photobucket.com/login";
            //BrowserSession browser = new BrowserSession_Forum();
            //HtmlDocument doc = browser.Get(url);
            //string ts = GetTS(doc);
            //string sig = GetSIG(doc);
            //browser.PostParams = "ts=" + ts +
            //                    "&sig=" + sig +
            //                    "&loginForm[redir]=" +
            //                    "&loginForm[redirectAction]=" +
            //                    "&loginForm[redirectParams]=" +
            //                    "&loginForm[returnUrl]=" +
            //                    "&action=" + "login" +
            //                    "&loginForm[usernameemail]=" + username +
            //                    "&loginForm[password]=" + password +
            //                    "&loginForm[rememberOnOff]=" + "on";
            //HtmlDocument loginDoc = browser.Post(loginUrl);

            //browser.HtmlWeb.Accept = "image/png,image/*;q=0.8,*/*;q=0.5";
            //browser.Get("http://s276.photobucket.com/upload/albums/" + username + "/?locaion=" + albumName, "image/png,image/*;q=0.8,*/*;q=0.5");
            
            ////Upload image
            //browser.PostParams = "location=" + albumName +
            //                    "&index=" + "0" +
            //                    "&the_web=" + imageUrl;
            //string folderUpload = "http://s276.photobucket.com/upload/api/albums/" + username + "/" + albumName.ToLower().Replace(" ", "%20") + "/";
            //HtmlDocument UploadDoc = browser.Post(folderUpload, "text/plain, */*; q=0.01");

            ////finalize upload
            //string finalizeUpload = "http://s276.photobucket.com/upload/api/albums/" + username + "/" + "finalizeupload" + "/";
            //browser.PostParams = "media=" + "images" +
            //                    "&doautoshare=" +
            //                    "&stat[]=" + "ok" +
            //                    "&json=" + "1" +
            //                    "&filename[]=" + imageName +
            //                    "&errormsg[]=" +
            //                    "&errorcode[]=";
            //HtmlDocument finalizeDoc = browser.Post(finalizeUpload, "*/*");

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //PhotobucketAPI.Client cli = new PhotobucketAPI.Client();
            //cli.makeQueryStringRequest(Client.RequestMethods.GET, "/ping", new Dictionary<string,string>() {
            //    {"foo", "bar"},
            //    {"oof", "rab"}
            //}, loading_callback);
            //cli.makePost("/ping", new Dictionary<string, string>() {
            //    {"foo", "bar"},
            //    {"oof", "rab"}
            //}, loading_callback);

            //PhotobucketNet.Photobucket pbn = new Photobucket("149831781", "2c6378577c48a52d7e48a656bfc08d5d"); //hgtuan2003
            PhotobucketNet.Photobucket pbn = new Photobucket("149831809", "0846f75902c3e45aeb742ceda49a9b9d"); //mrforumvn
            pbn.LaunchUserLogin();
            pbn = LoginPhotobucket(pbn);

            System.Diagnostics.Debug.WriteLine(pbn.Username);
            System.Diagnostics.Debug.WriteLine("Token:" + pbn.UserToken.Token);
            System.Diagnostics.Debug.WriteLine("Token Secret:" + pbn.UserToken.TokenSecret);
            System.Diagnostics.Debug.WriteLine("Username:" + pbn.UserToken.Username);
            System.Diagnostics.Debug.WriteLine("Subdomain:" + pbn.UserToken.Subdomain);
            System.Diagnostics.Debug.WriteLine("Home URL:" + pbn.UserToken.UserHomeUrl);
            string imgName = "test_pic";
            string imgTitle = "Test Pic";
            string albumName = "Movies";
            MediaItem mi = new MediaItem(pbn.Username, imgName, System.IO.File.Open("E:\\test_pic.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read), MediaType.Image);
            mi.Title = imgTitle;
            //mi = pbn.UploadMediaItemToBaseAlbum(mi);
            mi = pbn.UploadMediaItemToAlbum(mi, new Album(pbn.Username + "/" + albumName));
            
        }

        //public void loading_callback(Client.ResponseArgs args)
        //{
        //    System.Diagnostics.Debug.WriteLine(args.ResponseXML.ToString());
        //}

        protected void Button2_Click(object sender, EventArgs e)
        {
            //PhotobucketAPI.Client cli = new PhotobucketAPI.Client();
            //cli.makeQueryStringRequest(Client.RequestMethods.GET, "/ping", new Dictionary<string, string>() {
            //    {"foo", "bar"},
            //    {"oof", "rab"}
            //}, loading_callback);
            //cli.makePost("/login/request", new Dictionary<string, string>(), loading_callback);
            //cli.makePost("/apilogin/login?oauth_token=" + cli.oauth_token, new Dictionary<string, string>(), loading_callback);
        }

        public Photobucket LoginPhotobucket(Photobucket pbn)
        {
            try
            {
                System.Threading.Thread.Sleep(2000);
                pbn.RequestUserToken();
            }
            catch
            {
                pbn = LoginPhotobucket(pbn);
            }
            return pbn;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            PhotobucketNet.Photobucket pbn = new Photobucket("149831809", "0846f75902c3e45aeb742ceda49a9b9d"); //mrforumvn
            pbn.LaunchUserLogin();
        }

    }
}